/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InformationDetail.java
 *
 * Created on 02/04/2011, 23:31:00
 */
package net.stuffrepos.bricknetcenter.gui.informations;

import java.awt.Component;
import java.awt.Container;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import net.stuffrepos.bricknetcenter.database.schema.BlobInformation;
import net.stuffrepos.bricknetcenter.database.schema.Information;

/**
 *
 * @author Eduardo H. Bogoni <eduardobogoni@gmail.com>
 */
public class InformationDetail extends javax.swing.JPanel {

    private final InformationsFrame informationsFrames;

    InformationDetail(InformationsFrame informationsFrame) {
        initComponents();
        getPropertiesPanel().setLayout(new BoxLayout(getPropertiesPanel(), BoxLayout.PAGE_AXIS));
        this.informationsFrames = informationsFrame;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        propertiesScrollPane = new javax.swing.JScrollPane();
        properties = new javax.swing.JPanel();

        javax.swing.GroupLayout propertiesLayout = new javax.swing.GroupLayout(properties);
        properties.setLayout(propertiesLayout);
        propertiesLayout.setHorizontalGroup(
            propertiesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 398, Short.MAX_VALUE)
        );
        propertiesLayout.setVerticalGroup(
            propertiesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 298, Short.MAX_VALUE)
        );

        propertiesScrollPane.setViewportView(properties);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(propertiesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(propertiesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private Container getPropertiesPanel() {
        return this.properties;
    }

    public void setInformation(Information information) {
        getPropertiesPanel().removeAll();
        JLabel dn = new JLabel(information.getDn());
        getPropertiesPanel().add(dn);

        for (Information child : information.getInformations()) {
            if (child.getInformations().isEmpty()) {
                getPropertiesPanel().add(getChildComponent(child));
            }
        }

        getPropertiesPanel().validate();
    }

    private Component getChildComponent(Information child) {
        if (child instanceof BlobInformation) {
            try {
                //JLabel label = new JLabel(ImageIO.read((BlobInformation)child));
                BlobInformation blobInformation = (BlobInformation) child;
                BufferedImage image = ImageIO.read(new ByteArrayInputStream(blobInformation.getContent()));
                return new JLabel(new ImageIcon(image));
            } catch (Exception ex) {
                return new JLabel(child.getType() + " (Failed): " + child.getValue());
            }
        } else {
            return new JLabel(child.getType() + ": " + child.getValue());
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel properties;
    private javax.swing.JScrollPane propertiesScrollPane;
    // End of variables declaration//GEN-END:variables
}
